#include "Follows.h"

using namespace std;

//FollowsDataRow
FollowsDataRow::FollowsDataRow(int beingFollowed, int follower) {
	this->beingFollowed = beingFollowed;
	this->follower = follower;
}

void FollowsDataRow::setBeingFollowed(int beingFollowed) {
	this->beingFollowed = beingFollowed;
}

void FollowsDataRow::setFollower(int follower) {
	this->follower = follower;
}

int FollowsDataRow::getBeingFollowed(){
	return this->beingFollowed;
}

int FollowsDataRow::getFollower(){
	return this->follower;
}

//Follows
vector<FollowsDataRow> Follows::table = vector<FollowsDataRow>();

Follows::Follows() {
}

void Follows::setFollowsRelation(int beingFollowed, int follower){
	table.push_back(FollowsDataRow(beingFollowed, follower));
}

bool Follows::isFollowsRelation(int beingFollowed, int follower){
	for(size_t i = 0; i < table.size(); i++) {
		if (table[i].getBeingFollowed() == beingFollowed && table[i].getFollower() == follower) {
			return true;
		}
	}
	return false;
}

bool Follows::isFollowsStarRelation(int beingFollowed, int follower){
	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getBeingFollowed() == beingFollowed && table[i].getFollower() == follower) {
			return true;
		} 
		if (table[i].getBeingFollowed() == beingFollowed) {
			return isFollowsStarRelation(table[i].getFollower(), follower);
		}
	}
	return false;
}

int Follows::getFollower(int beingFollowed){
	for(size_t i = 0; i < table.size(); i++) {
		if (table[i].getBeingFollowed() == beingFollowed) {
			return table[i].getFollower();
		}
	}
	return -1;
}

int Follows::getBeingFollowed(int follower){
	for(size_t i = 0; i < table.size(); i++) {
		if (table[i].getFollower() == follower) {
			return table[i].getBeingFollowed();
		}
	}
	return -1;
}

vector<int> Follows::getFollowerStar(int beingFollowed){
	vector<int> list, temp;
	
	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getBeingFollowed() == beingFollowed) {
			list.push_back(table[i].getFollower());
			temp = getFollowerStar(table[i].getFollower());
			if (temp.size() != 0) {
				list.insert(list.end(), temp.begin(), temp.end()); //push all results to list
			}
		}
	}

	return list;
}

vector<int> Follows::getBeingFollowedStar(int follower){
	vector<int> list, temp; //reverse;

	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getFollower() == follower) {
			list.push_back(table[i].getBeingFollowed());
			temp = getBeingFollowedStar(table[i].getBeingFollowed());
			if (temp.size() != 0) {
				list.insert(list.end(), temp.begin(), temp.end()); //push all results to list
			}
		}
	}
	//reverse(list.begin(), list.end());
	return list;
}

//for unit testing
FollowsDataRow Follows::getLastDataRowFromTable() {
	return table[table.size() - 1];
}
